package cn.tedu.straw.portal.controller;

import cn.tedu.straw.portal.ex.InsertException;
import cn.tedu.straw.portal.ex.NameDuplicateException;
import cn.tedu.straw.portal.ex.ServiceException;
import cn.tedu.straw.portal.util.R;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({ServiceException.class, FileUploadException.class})
    public R handleException(Exception e) {
        if (e instanceof InsertException) {
            return R.failure(R.State.ERR_INSERT, e);
        } else if (e instanceof NameDuplicateException) {
            return R.failure(R.State.ERR_USERNAME_DUPLICATE, e);
        }
        e.printStackTrace();
        return R.failure(-999999, "出现了未知错误，请检查控制台！");
    }

}
